package com.zreal.generator.codegenerate.database;

import com.zreal.generator.util.StringUtils;

public class TableConvert {
    public static String getNullAble(String nullable) {
        if (("YES".equals(nullable)) || ("yes".equals(nullable))
                || ("y".equals(nullable)) || ("Y".equals(nullable))
                || ("t".equals(nullable))) {
            return "Y";
        }
        if (("NO".equals(nullable)) || ("N".equals(nullable))
                || ("no".equals(nullable)) || ("n".equals(nullable))
                || ("f".equals(nullable))) {
            return "N";
        }
        return null;
    }

    public static String getV(String s) {
        return "'" + s + "'";
    }

    public static String formatDataType(String dataType, String precision, String scale) {
        if (dataType.contains("char")) {
            dataType = "String";
        } else if (dataType.contains("bigint")) {
            dataType = "Long";
        } else if (dataType.contains("int")) {
            dataType = "Integer";
        } else if (dataType.contains("float")) {
            dataType = "Float";
        } else if (dataType.contains("double")) {
            dataType = "Double";
        } else if (dataType.contains("number") && "0".equals(scale) && Integer.valueOf(precision) < 5) {
            dataType = "Integer";
        } else if (dataType.contains("number") && "0".equals(scale)) {
            dataType = "Long";
        } else if (dataType.contains("number")) {
            dataType = "BigDecimal";
        } else if (dataType.contains("decimal")) {
            dataType = "BigDecimal";
        } else if (dataType.contains("date")) {
            dataType = "Date";
        } else if (dataType.contains("time")) {
            dataType = "Date";
        } else if (dataType.contains("clob")) {
            dataType = "String";
        } else if (dataType.contains("blob")) {
            dataType = "Blob";
        } else if (dataType.contains("numeric")) {
            dataType = "BigDecimal";
        } else {
            dataType = "Object";
        }
        return dataType;
    }

    /**
     * 表名驼峰方式显示根据"_"下划线转换
     *
     * @param tableName 表名
     * @return 以驼峰方式返回名称
     */
    public static String formatTableName(String tableName) {
        String reTableName = tableName.toLowerCase();
        if (reTableName.lastIndexOf("_") > -1) {
            String parts[] = reTableName.split("_");
            String name = "";
            for (int i = 0; i < parts.length; i++) {
                String tempStr = parts[i];
                if(StringUtils.isEmpty(tempStr)){
                    continue;
                }
                tempStr = tempStr.substring(0, 1).toUpperCase() + tempStr.substring(1, tempStr.length());
                name += tempStr;
            }
            reTableName = name;
        }else{
            reTableName = reTableName.substring(0, 1).toUpperCase() + reTableName.substring(1);
        }
        return reTableName;
    }

    public static String formatColumnName(String columnName) {
        String reColumn = columnName.toLowerCase();
        return reColumn;
    }

    //驼峰
    public static String formatColumnNameT(String columnName) {
        String reColumn = columnName.toLowerCase();
        //reColumn =reColumn.substring(reColumn.lastIndexOf("_")+1);
        String parts[] = reColumn.split("_");
        int len = parts.length;
        reColumn = "";
        for (int i = 0; i < len; i++) {
            if (i > 0) {
                String tempStr = parts[i];
                tempStr = tempStr.substring(0, 1).toUpperCase() + tempStr.substring(1, tempStr.length());
                reColumn += tempStr;
            } else {
                reColumn += parts[i];
            }
        }
        return reColumn;
    }

    public static String formatDBFieldType(String fieldType) {
        if ("DATETIME".equalsIgnoreCase(fieldType) && "mysql".equals(DataSource.dataSource.getDbType())) {
            return "DATE";
        } else if ("INT".equals(fieldType) && "mysql".equals(DataSource.dataSource.getDbType())) {
            return "INTEGER";
        }
        return fieldType.toUpperCase();
    }
}
